package cn.itcast.eduservice.controller.front;

import cn.itcast.commonutils.JwtUtils;
import cn.itcast.commonutils.R;
import cn.itcast.commonutils.ordervo.CourseWebVoOrder;
import cn.itcast.eduservice.client.OrderClient;
import cn.itcast.eduservice.entity.EduCourse;
import cn.itcast.eduservice.entity.chapter.ChapterVo;
import cn.itcast.eduservice.entity.frontvo.CourseFrontVo;
import cn.itcast.eduservice.entity.frontvo.CourseWebVo;
import cn.itcast.eduservice.service.EduChapterService;
import cn.itcast.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Api(description = "课程前台")
@RestController
@RequestMapping("/eduservice/coursefront")
@CrossOrigin
public class CourseFrontController {
    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private OrderClient orderClient;

    //1 条件查询带分页查询课程
    @PostMapping("getFrontCourseList/{page}/{limit}")
    public R getFrontCourseList(@PathVariable long page, @PathVariable long limit, @RequestBody(required = false)CourseFrontVo courseFrontVo){
        // 创建分页对象
        Page<EduCourse> pageCourse = new Page<>(page,limit);
        Map<String,Object> map = courseService.getCourseFrontList(pageCourse,courseFrontVo);
        // 返回分页所有数据
        return R.ok().data(map);
    }

    // 课程详情的方法
    @GetMapping("getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request) {
        // 根据课程id，编写sql语句查询课程信息
        CourseWebVo courseWebVo = courseService.getBaseCourseInfo(courseId);

        // 根据课程id查询章节和小节
        List<ChapterVo> chapterVideoList = chapterService.getChapterVideoById(courseId);

        boolean isBuyCourse = orderClient.isBuyCourse(courseId, JwtUtils.getMemberIdByJwtToken(request));

        return R.ok().data("courseWebVo",courseWebVo).data("chapterVideoList",chapterVideoList).data("isBuyCourse",isBuyCourse);
    }

    // 根据课程id，查询课程基本信息
    @ApiOperation(value = "根据课程id获取个人基本信息")
    @PostMapping("/getCourseInfo/{courseId}")
    public CourseWebVoOrder getCourseInfo(@PathVariable String courseId){
        CourseWebVo baseCourseInfo = courseService.getBaseCourseInfo(courseId);

        CourseWebVoOrder courseInfo = new CourseWebVoOrder();

        BeanUtils.copyProperties(baseCourseInfo,courseInfo);

        return courseInfo;
    }
}
